package com.example.mediaservice;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import androidx.core.app.NotificationCompat;
import androidx.media.session.MediaButtonReceiver;

public class NotificationManagerHelper {
    private static final String CHANNEL_ID = "media_channel";
    private static final int NOTIFICATION_ID = 1;

    private Context context;
    private MediaSessionCompat mediaSession;

    public NotificationManagerHelper(Context context, MediaSessionCompat mediaSession) {
        this.context = context;
        this.mediaSession = mediaSession;
    }

    public void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "媒体播放",
                    NotificationManager.IMPORTANCE_LOW
            );
            NotificationManager manager = context.getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }
    }

    //在 Android 系统的通知栏中显示媒体播放控制界面
    public Notification createNotification(boolean isPlaying) {
        // 创建通知点击意图
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 0, intent,
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
        );

        // 获取当前媒体信息
        MediaMetadataCompat metadata = mediaSession.getController().getMetadata();
        String title = "未知歌曲";
        String artist = "未知艺术家";

        if (metadata != null) {
            title = metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
            artist = metadata.getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
        }

        // 构建一个媒体播放控制通知，让用户可以在通知栏中控制媒体播放
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(artist)
                .setSmallIcon(R.drawable.img)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setMediaSession(mediaSession.getSessionToken())
                        .setShowActionsInCompactView(0, 1, 2))
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        // 添加上一首按钮
        builder.addAction(new NotificationCompat.Action(
                android.R.drawable.ic_media_previous, "上一首",
                MediaButtonReceiver.buildMediaButtonPendingIntent(
                        context, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));

        // 添加播放/暂停按钮
        if (isPlaying) {
            builder.addAction(new NotificationCompat.Action(
                    android.R.drawable.ic_media_pause, "暂停",
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            context, PlaybackStateCompat.ACTION_PAUSE)));
        } else {
            builder.addAction(new NotificationCompat.Action(
                    android.R.drawable.ic_media_play, "播放",
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            context, PlaybackStateCompat.ACTION_PLAY)));
        }

        // 添加下一首按钮
        builder.addAction(new NotificationCompat.Action(
                android.R.drawable.ic_media_next, "下一首",
                MediaButtonReceiver.buildMediaButtonPendingIntent(
                        context, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));

        return builder.build();
    }

    public int getNotificationId() {
        return NOTIFICATION_ID;
    }

    public String getChannelId() {
        return CHANNEL_ID;
    }
}
